{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "a = 'hello world'\n",
    "b = \"abcdefg\"\n",
    "print(type(a))\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "name1 = 'Tom'\n",
    "name2 = \"Rose\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "name3 = ''' Tom '''\n",
    "name4 = \"\"\" Rose \"\"\"\n",
    "a = ''' i am Tom, \n",
    "        nice to meet you! '''\n",
    "\n",
    "b = \"\"\" i am Rose, \n",
    "        nice to meet you! \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "我的名字是Tom\n",
      "我的名字是Tom\n"
     ]
    }
   ],
   "source": [
    "print('hello world')\n",
    "\n",
    "name = 'Tom'\n",
    "print('我的名字是%s' % name)\n",
    "print(f'我的名字是{name}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的名字：帅哥\n",
      "您输入的名字是帅哥\n",
      "<class 'str'>\n",
      "请输入您的密码：123456\n",
      "您输入的密码是123456\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "name = input('请输入您的名字：')\n",
    "print(f'您输入的名字是{name}')\n",
    "print(type(name))\n",
    "\n",
    "password = input('请输入您的密码：')\n",
    "print(f'您输入的密码是{password}')\n",
    "print(type(password))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b\n",
      "a\n",
      "c\n"
     ]
    }
   ],
   "source": [
    "name = \"abcdef\"\n",
    "\n",
    "print(name[1])\n",
    "print(name[0])\n",
    "print(name[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cde\n",
      "cde\n",
      "abcde\n",
      "bcdefg\n",
      "abcdefg\n",
      "aceg\n",
      "abcdef\n",
      "def\n",
      "gfedcba\n"
     ]
    }
   ],
   "source": [
    "name = \"abcdefg\"\n",
    "\n",
    "print(name[2:5:1])  # cde\n",
    "print(name[2:5])  # cde\n",
    "print(name[:5])  # abcde\n",
    "print(name[1:])  # bcdefg\n",
    "print(name[:])  # abcdefg\n",
    "print(name[::2])  # aceg\n",
    "print(name[:-1])  # abcdef, 负1表示倒数第一个数据\n",
    "print(name[-4:-1])  # def\n",
    "print(name[::-1])  # gfedcba"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 常用操作\n",
    "\n",
    "### 查找"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "23\n",
      "35\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "mystr = \"hello world and itcast and itheima and Python\"\n",
    "\n",
    "print(mystr.find('and'))  # 12\n",
    "print(mystr.find('and', 15, 30))  # 23\n",
    "print(mystr.rfind(\"and\"))  # 35\n",
    "print(mystr.find('ands'))  # -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "23\n"
     ]
    }
   ],
   "source": [
    "mystr = \"hello world and itcast and itheima and Python\"\n",
    "\n",
    "print(mystr.index('and'))  # 12\n",
    "print(mystr.index('and', 15, 30))  # 23"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "substring not found",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-9d0eb41b5e07>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmystr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'ands'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: substring not found"
     ]
    }
   ],
   "source": [
    "print(mystr.index('ands'))  # 报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "mystr = \"hello world and itcast and itheima and Python\"\n",
    "\n",
    "print(mystr.count('and'))  # 3\n",
    "print(mystr.count('ands'))  # 0\n",
    "print(mystr.count('and', 0, 20))  # 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world he itcast he itheima he Python\n",
      "hello world he itcast he itheima he Python\n",
      "hello world and itcast and itheima and Python\n"
     ]
    }
   ],
   "source": [
    "mystr = \"hello world and itcast and itheima and Python\"\n",
    "\n",
    "# 结果：hello world he itcast he itheima he Python\n",
    "print(mystr.replace('and', 'he'))\n",
    "# 结果：hello world he itcast he itheima he Python\n",
    "print(mystr.replace('and', 'he', 10))\n",
    "# 结果：hello world and itcast and itheima and Python\n",
    "print(mystr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello world ', ' itcast ', ' itheima ', ' Python']\n",
      "['hello world ', ' itcast ', ' itheima and Python']\n",
      "['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']\n",
      "['hello', 'world', 'and itcast and itheima and Python']\n"
     ]
    }
   ],
   "source": [
    "mystr = \"hello world and itcast and itheima and Python\"\n",
    "\n",
    "# 结果：['hello world ', ' itcast ', ' itheima ', ' Python']\n",
    "print(mystr.split('and'))\n",
    "# 结果：['hello world ', ' itcast ', ' itheima and Python']\n",
    "print(mystr.split('and', 2))\n",
    "# 结果：['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']\n",
    "print(mystr.split(' '))\n",
    "# 结果：['hello', 'world', 'and itcast and itheima and Python']\n",
    "print(mystr.split(' ', 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "空格是会保留的！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chuan_zhi_bo_ke\n",
      "aa...b...cc...ddd\n"
     ]
    }
   ],
   "source": [
    "list1 = ['chuan', 'zhi', 'bo', 'ke']\n",
    "t1 = ('aa', 'b', 'cc', 'ddd')\n",
    "# 结果：chuan_zhi_bo_ke\n",
    "print('_'.join(list1))\n",
    "\n",
    "# 结果：aa...b...cc...ddd\n",
    "print('...'.join(t1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world and itcast and itheima and python\n"
     ]
    }
   ],
   "source": [
    "mystr = \"hello world and itcast and itheima and Python\"\n",
    "\n",
    "# 结果：Hello world and itcast and itheima and python\n",
    "print(mystr.capitalize())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World And Itcast And Itheima And Python\n"
     ]
    }
   ],
   "source": [
    "mystr = \"hello world and itcast and itheima and Python\"\n",
    "\n",
    "# 结果：Hello World And Itcast And Itheima And Python\n",
    "print(mystr.title())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world and itcast and itheima and python\n"
     ]
    }
   ],
   "source": [
    "mystr = \"hello world and itcast and itheima and Python\"\n",
    "\n",
    "# 结果：hello world and itcast and itheima and python\n",
    "print(mystr.lower())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON\n"
     ]
    }
   ],
   "source": [
    "mystr = \"hello world and itcast and itheima and Python\"\n",
    "\n",
    "# 结果：HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON\n",
    "print(mystr.upper())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:root] *",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
